package kr.co.mustgo.model.dao.membership;

import java.sql.SQLException;
import java.util.List;

import kr.co.mustgo.domain.membership.Admin;
import kr.co.mustgo.domain.membership.Membership;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class AdminDAOImpl implements AdminDAO{

		/*
		 * SqlSessionTemplate : mybatis의 SqlSession 역할.
		 * 		- SqlSession의 JDBC 관련 메소드와 같은 메소드들을 제공.
		 * 		- SqlSession과의 차이점 - Transaction 처리(commit/rollback)
		 * 			- SqlSession : 직접처리
		 * 			- SqlSessionTemplate : Spring 컨테이너에게 맞긴다.
		 * 		- 생성자를 통해 SqlSessionFactoryBean을 주입받아야 한다.
		 * 			- SqlSessionFactoryBean - SqlSessionFactory를 생성하는 객체.
		 */
		
		@Override
		public Admin selectAdminById(String id) throws SQLException {
			return session.selectOne(makeSqlId("selectAdminById"), id);
		}
	
		
	
		@Autowired //변수에 @Autowired 할 경우 생성자 필요없음(안지워도됨?)
		private SqlSessionTemplate session;
		
//		public void setSqlSessionTemplate(SqlSessionTemplate session){
//			this.session = session;
//		}
		
		@Override
		public int insertAdmin(Admin admin) throws SQLException {
			return session.insert(makeSqlId("insertAdmin"), admin);
		}

		@Override
		public int updateAdmin(Admin admin) throws SQLException {
			return session.update(makeSqlId("updateAdmin"), admin);
		}

		@Override
		public int deleteAdminById(String id) throws SQLException {
			return session.delete(makeSqlId("deleteAdminById"), id);
		}

		@Override
		public List<Admin> selectAdminList() throws SQLException {
			return session.selectList(makeSqlId("selectAdminList"));
		}

		@Override
		public List<Admin> selectAdminByName(String name) throws SQLException {
			return session.selectList(makeSqlId("selectAdminByName"), name);
		}


		
		@Override
		//총 회원의 수 조회
		public int selectRowCount() throws SQLException {
			return session.selectOne(makeSqlId("selectRowCount"));
		}
		
		private String makeSqlId(String id){
			return "admin.manager."+id;
		}
	
}//end class
